<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>
<form class="layui-form" lay-filter="menu">
  <div id="id-item" class="layui-form-item">
    <label class="layui-form-label" for="id">id</label>
    <div class="layui-input-inline">
      <input id="id" readonly type="text" name="id" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label" for="type">类型</label>
    <div class="layui-input-inline">
      <select id="type" name="type" lay-verify="required">
        <option value="">请选择</option>
        <option value="DIRECTORY">目录</option>
        <option value="MENU">菜单</option>
      </select>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label" for="parent">父菜单</label>
    <div class="layui-input-inline">
      <select id="parent" name="parent">
        <option value="">/</option>
      </select>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label" for="name">菜单名称</label>
    <div class="layui-input-inline">
      <input id="name" type="text" name="name" lay-verify="required" lay-reqtext="菜单名是必填项，岂能为空？" placeholder="请输入菜单名"
             autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label" for="code">code</label>
    <div class="layui-input-inline">
      <input id="code" type="text" name="code" placeholder="请输入编码" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label" for="url">url</label>
    <div class="layui-input-inline">
      <input id="url" type="text" name="url" placeholder="请输入url" autocomplete="off"
             class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label" for="state">是否启用</label>
    <div class="layui-input-inline">
      <input id="state" type="checkbox" name="state" lay-skin="switch" lay-text="ON|OFF" checked>
    </div>
  </div>
  
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label" for="remark">备注</label>
    <div class="layui-input-block">
      <textarea id="remark" name="remark" placeholder="请输入" class="layui-textarea"></textarea>
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button id="insert" class="layui-btn" lay-submit lay-filter="insert">立即提交</button>
      <button id="update" class="layui-btn" lay-submit lay-filter="update">立即提交</button>
      <button id="reset" type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
<script src="/layui/layui.js" charset="utf-8"></script>
<script src="/jquery/jquery-3.5.1.min.js"></script>
<script>
  $.ajaxSetup({
    dataType: "json",
    contentType: "application/json",
  });
  // 先初始化
  layui.use(['form', 'layer'], function () {
    const form = layui.form;
    const layer = layui.layer;
    // 新增
    form.on('submit(insert)', function (data) {
      const params = JSON.stringify(data.field);
      $.ajax({
        url: "/api/menu",
        method: 'post',
        contentType: 'application/json',
        data: params,
        dataType: 'json',
        success: function (data) {
          if (data.code === 200) {
            layer.msg('添加成功')
            // 1秒后刷新
            setTimeout(function () {
              window.parent.location.reload();
            }, 1500);
          } else if (data.code === 100) {
            layer.msg(data.message);
          };
        }
      });
      return false;
    });
  
    $.get("/api/menu/parent", function (data) {
      console.log()
      if (data.code === 200){
        const options = data.data;
        for (let i = 0; i < options.length; i++) {
          const id = options[i].id;
          if ($("#id").val() == id){
            continue;
          }
          const name = options[i].name+"("+options[i].code+")";
          $("#parent").append("<option value='"+id+"'>"+name+"</option>");
        }
      }
      form.render('select');
    })
    
    // 修改
    form.on('submit(update)', function (data) {
      const params = JSON.stringify(data.field);
      const id = data.field.id;
      $.ajax({
        url: "/api/menu/" + id,
        method: 'put',
        contentType: 'application/json',
        data: params,
        dataType: 'json',
        success: function (data) {
          if (data.code === 200) {
            layer.msg('修改成功')
            setTimeout(function () {
              window.parent.location.reload();
            }, 1500);
          } else if (data.code === 100) {
            layer.msg(data.message);
          }
        }
      });
      return false;
    })
  });
  
  // 最后赋值
  function formData(data) {
    layui.use(['form', 'layer'], function () {
      const form = layui.form;
      form.val("menu", JSON.parse(JSON.stringify(data)));
      $("#parent").select(data.parent);
    });
  }
</script>
</body>
</html>